package com.kylin.module.user.actions;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.util.List;
import java.util.Locale;

//import java.util.ResourceBundle;
//import java.util.Locale;
//import com.kylin.common.util.ShortResourceBundle;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.hibernate.criterion.Order;

import com.kylin.common.Constants;
import com.kylin.module.community.orm.Member;
import com.kylin.common.paginate.DisplayPage;
import com.kylin.hibernate.CriteriaInfo;

import com.kylin.module.shop.business.CompanyBlogic;
import com.kylin.module.user.business.UserBlogic;
import com.kylin.module.user.orm.User;

//import com.kylin.module.admin.form.CatDictForm;
import com.kylin.module.community.business.MemberBlogic;
import com.kylin.module.community.forms.MemberForm;
import com.kylin.struts.BaseActionInterface;
import com.kylin.struts.BaseDispatchAction;

import com.kylin.common.entity.MsgKey;
import com.kylin.common.exception.BusinessException;
import com.kylin.common.util.MemObjFactory;


public class UserAction  extends BaseDispatchAction implements BaseActionInterface {

	protected static Logger log = Logger.getLogger(UserAction.class);	
	
	public ActionForward unspecified(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) {
		log.debug("in user.unspecified");
		return this.login(mapping, actionForm, request, response);
	}

	public ActionForward login(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) {
		
		log.info("in user.login, para: " + request.getParameter("forward"));	
		
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		String forward =  request.getParameter("forward");
		String uid =  request.getParameter("uid");
		if (forward == "" || forward == null){
			forward = "default";
		}
	
		try{
			UserBlogic bean = new UserBlogic();
			User user = bean.loginUser(username, password);	
			if (user != null){
				request.getSession().setAttribute(Constants.CURR_USER, user);
			}
			else{
				/*
				 *  username or password not correct
				 */
				forward = "login";
			}
			
		}catch (Exception e) {
			log.info("login exception");
			forward = doException( request, e, forward);
		}
		log.info("login forward: "+forward);

		if (request.getParameter("uid") != null){
			if (forward.equals("msg.add") ){
			
				String path = "/module/community/sitemsg/sitemsg.do?task=add&uid=" + request.getParameter("uid");
				log.info("path: "+path);
				ActionForward actionforward = new ActionForward(path);
				actionforward.setRedirect(true);
				return actionforward;	
			
			}else if(forward.equals("friend.add") ){
	
				String path = "/module/community/friendslist/friendslist.do?task=add&uid=" + request.getParameter("uid");
				log.info("path: "+path);
				ActionForward actionforward = new ActionForward(path);
				actionforward.setRedirect(true);
				return actionforward;				
			}
			else{
				
				return mapping.findForward(forward);
			}
		}
		else{
			log.info("else");
			return mapping.findForward(forward);
			
		}
	}	

	public ActionForward logout(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) {
		//log.debug("in user.logout");		
		String forward = "default";
		User user = (User)request.getSession().getAttribute(Constants.CURR_USER);
		String path ="/module/community/post/post.do?task=cats";		
		try{		
			if (user != null ){	
				if(user.getUserrole().equals(Constants.USER_ROLE_COMPANY)){
					path ="/homepage/frame.jsp";
				}				
				request.getSession().removeAttribute(Constants.CURR_USER);
			}			
		}catch (Exception e) {
			forward = doException( request, e, forward);
		}
		log.info("logout path: "+path);
		ActionForward actionforward = new ActionForward(path);
		actionforward.setRedirect(true);		
		return actionforward;			
		
		//return mapping.findForward(forward);
	}	
	
	public ActionForward changePassword(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response){
		String forward = "message";
		UserBlogic blogic = new UserBlogic();
		log.info(request.getParameter("userId") + " " +request.getParameter("oldPassword") + " " + request.getParameter("newPassword"));
		try {
			blogic.changePassword(request.getParameter("userId"), request.getParameter("oldPassword"), request.getParameter("newPassword"));
			log.debug("密码已修改" );
			// 返回成功的显示信息，需要在结果页面添加 <mytag:MyActionMsg />
	        request.setAttribute(Constants.MSGKEY, new MsgKey("","密码已修改"));
		}catch (Exception e) {
			forward = doException( request, e, forward);
		}
		//  /module/community/member/post.do?task=mypost
		
		
		String path = null;//	mapping.findForward("succ").getPath();
		path = "/module/community/member/post.do?task=mypost";
		log.info("path: "+path);

		ActionForward actionforward = new ActionForward(path);
		actionforward.setRedirect(true);
		return actionforward;	
		
	
		/*
		ActionMessages messages = new ActionMessages();
		messages.add("msg", new ActionMessage("修改密码成功"));			
		saveMessages(request, messages);		
		
		return mapping.findForward(forward);
		*/
	}
	public ActionForward delete(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub
		return null;
	}
	public ActionForward search(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub
		return null;
	}
	public ActionForward update(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub
		return null;
	}

	public ActionForward add(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub
		return null;
	}
	
}
